package com.shxc.smarttrade.screenshot;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;

/**
 * Created by wei on 16-9-18.
 * <p>
 * 完全透明 只是用于弹出权限申请的窗而已
 */
@ContentView(R.layout.part_transparent)
public class ScreenShotActivity extends BaseActivity {

    public static final int REQUEST_MEDIA_PROJECTION = 0x2893;


    @Override
    public void onCreate(Bundle savedInstanceState) {

//        setTheme(android.R.style.Theme_Dialog);//这个在这里设置 之后导致 的问题是 背景很黑
        super.onCreate(savedInstanceState);

        //如下代码 只是想 启动一个透明的Activity 而上一个activity又不被pause
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//        getWindow().setDimAmount(0f);

        requestScreenShot();
    }


    @SuppressLint("WrongConstant")
    public void requestScreenShot() {
        if (Build.VERSION.SDK_INT >= 21) {
            startActivityForResult(
                    ((MediaProjectionManager) getSystemService("media_projection")).createScreenCaptureIntent(),
                    REQUEST_MEDIA_PROJECTION
            );
        } else {
            ToastUtils.showToast("版本过低,无法截屏");
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_MEDIA_PROJECTION: {
                if (resultCode == -1 && data != null) {
                    Shotter shotter = new Shotter(ScreenShotActivity.this, data);
                    shotter.startScreenShot(new Shotter.OnShotListener() {
                        @Override
                        public void onFinish() {
                            ToastUtils.showToast("截屏已保存至内存根目录下！");
                            finish(); // don't forget finish activity
                        }
                    });
                }
            }
        }
    }


}